package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteNoteEsService;
import cn.wolfcode.wolf2w.business.api.domain.NoteEs;
import cn.wolfcode.wolf2w.business.service.INoteContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 旅游日记 Controller
 *
 * @author 王南棕
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/notes")
public class NoteController {
    @Autowired
    private INoteService noteService;
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    @Autowired
    private INoteContentService noteContentService;
    @Autowired
    private RemoteNoteEsService remoteNoteEsService;
    @PostMapping("/star/{nid}")
    public R<Map<String, Object>> star(@PathVariable Long nid) {
        Map<String, Object> map = noteService.star(nid);
        return R.ok(map);
    }

    @GetMapping("/isUserFavor")
    public R<Boolean> isUserFavor(Long nid, Long uid) {
        Boolean isUserFavor = noteService.isUserFavor(nid, uid);
        return R.ok(isUserFavor);
    }

    @PostMapping("/viewnumIncr")
    public R<Map<String, Object>> viewnumIncr(Long nid) {
        Map<String, Object> map = noteService.viewnumIncr(nid);
        return R.ok(map);
    }


    @RequiresLogin
    @PostMapping("/add")
    public R<?> add(@RequestBody Note note) {
        noteService.addNoteAndContent(note);
        return R.ok();
    }

    @PostMapping("/favor")
    public R<Map<String, Object>> collect(Long noteId){
        Map<String, Object> collect = noteService.collect(noteId);
        return R.ok(collect);
    }

    @GetMapping("/viewnnumTop3")
    public R<List<Note>> viewnnumTop3(Long destId){
        List<Note> list=noteService.viewnnumTop3(destId);
        return R.ok(list);
    }

    //es初始化
    @GetMapping("/dataInit")
    public R<?> dataInit(){
        List<Note> list = noteService.list();
        for (Note note : list) {
            NoteEs noteEs = new NoteEs();
            BeanUtils.copyProperties(note, noteEs);
            remoteNoteEsService.initNoteEs(noteEs, SecurityConstants.INNER);
        }
        return R.ok("初始化成功");
    }
    /**
     * 旅游日记详情
     */
    @GetMapping("/detail/{id}")
    public R<Note> detail(@PathVariable Long id) {
        Note note = noteService.getById(id);
        Long userId = note.getAuthorId();
        UserInfo data = remoteUserInfoService.getOne(userId, SecurityConstants.INNER).getData();
        note.setAuthor(data);
        note.setContent(noteContentService.getById(note.getId()));
        return R.ok(note);
    }
    /**
     * 旅游日记 列表
     */
    @GetMapping("/query")
    public R<IPage<Note>> query(NoteQuery qo) {
        IPage<Note> page = noteService.queryPage(qo);
        return R.ok(page);
    }
    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Note>> feignList() {
        return R.ok(noteService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Note> feignGet(@PathVariable Long id) {
        return R.ok(noteService.getById(id));
    }

    @GetMapping("/feign/noteStatisHashPersistence")
    public R<?> noteStatisHashPersistence(){
        noteService.noteStatisHashPersistence();
        return R.ok();
    }



}
